package com.alibaba.shopping_order_service.service;

import com.alibaba.shopping_common.pojo.CartGoods;
import com.alibaba.shopping_common.pojo.Orders;
import com.alibaba.shopping_common.service.OrderService;
import com.alibaba.shopping_order_service.mapper.CartGoodsMapper;
import com.alibaba.shopping_order_service.mapper.OrdersMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @ClassName OrderServiceImpl
 * @Description TODO
 * @Author Leo
 * @Date 2024/12/19 09:28
 */
@Transactional
@Service
@DubboService
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private CartGoodsMapper cartGoodsMapper;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    private final String CHECK_ORDERS_QUEUE = "check_orders_queue";

    /**
     *
     * @param orders
     * @return
     */
    @Override
    public Orders buildOrder(Orders orders) {
        // 将订单设置成未付款状态
        if (orders.getStatus() == null){
            orders.setStatus(1);
        }
        // 计算订单金额
        List<CartGoods> cartGoods = orders.getCartGoods();
        // 设置初始值为0
        BigDecimal sum = BigDecimal.ZERO;
        for (CartGoods cartGood : cartGoods) {
            // 获取商品数量
            BigDecimal num = new BigDecimal(cartGood.getNum());
            // 获取单件商品的价格
            BigDecimal price = cartGood.getPrice();
            // 数量*单价
            BigDecimal multiply = num.multiply(price);
            // 累加
            sum = sum.add(multiply);
        }
        // 设置订单创建时间
        orders.setCreateTime(new Date());
        // 保存订单
        ordersMapper.insert(orders);
        // 遍历所有商品
        for (CartGoods cartGood : cartGoods) {
            // 将订单ID写入订单商品中形成关联
            cartGood.setOrderId(orders.getId());
            // 保存订单商品
            cartGoodsMapper.insert(cartGood);
        }
        // rocketMQTemplate
        rocketMQTemplate.syncSend(
                // 消息队列
                CHECK_ORDERS_QUEUE,
                // 消息体
                MessageBuilder.withPayload(orders.getId()).build(),
                // 发送消息的超时时间
                15000,
                // 延迟等级 1-16级别:1s 5s 10s 30s 1m ....
                3);
        return orders;
    }

    /**
     * 更新订单(用户更新订单状态)
     * @param orders
     */
    @Override
    public void updateOrder(Orders orders) {
        ordersMapper.updateById(orders);
    }

    /**
     * 根据ID查询订单详情
     * @param id 订单ID
     * @return
     */
    @Override
    public Orders findById(String id) {
        return ordersMapper.findOrdersById(id);
    }

    /**
     * 查询用户所有的订单
     * @param userId 用户ID
     * @param status 订单状态
     * @return
     */
    @Override
    public List<Orders> findUserOrders(Long userId, Integer status) {
        return ordersMapper.findOrderByUserIdAndStatus(userId,status);
    }
}




